﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dev.Control;
using SfcorpDLL.biz;
using SfcorpDLL.ety;
using TT.Common;
using SQLHelper;
using DevExpress.XtraBars;
using TT.ORM;

namespace Sfcorp.form
{
    public partial class frmClient : BaseFormDev
    {
        bizClient bzClient;
        public frmClient()
        {
            InitializeComponent();
        }

        private string formSta;
        public string FormSta
        {
            set
            {
                formSta = value;
                switch (formSta)
                {
                    case "A":
                        lblBar.Caption = "新建状态";
                        break;
                    case "E":
                        lblBar.Caption = "编辑状态";
                        break;
                    default:
                        break;
                }
            }
            get
            {
                return formSta;
            }
        }
        #region 事件

        private void frmClient_Load(object sender, EventArgs e)
        {
            bzClient = new bizClient(global.basSysPara);

            //刷新界面所需要的数据
            TT.Module.RefreshTable.BaseTableRefreshByTables("CODE_REF,C_COLLABORATE",false);

            #region 下拉数据
            cboEnable_Flag.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilCustomerEnabled);
            cboCollaborate_Id.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilCollaboRate);
            cboCustomer_Id.DataSource = dataProcessor.CopyIList<SfcorpDLL.ety.etyCustomer>(global.ilCustomer);
            cboFee_Way_Id.DataSource = dataProcessor.CopyIList<etyCodeRef>(global.ilFeeWay);
        
            #endregion

            #region 表格数据
            ggcClient.GridView = this.gridView1;
            ggcClient.GridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.Click;
            string sMapingCols = "CLIENT_ID,CLIENT_NAM,ENABLE_FLAG,CLIENT_CONTACT,CONTRACT_DATE_VAL_BEG,CONTRACT_DATE_VAL_END,COLLABORATE_ID,CUSTOMER_NAM,FEE_WAY_NAM,CLIENT_CONTACT1,SEQ_NO";
            string sMapingHeaderTxt = "办单人代码,办单人名称,生效状态,联系方式,有效开始日期,有效结束日期,合作方代码,客户名称,结算方式,固话联系方式,排列顺序";
            ggcClient.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt);
            this.winUtil.LoadGridLayout(ggcClient);

            RefreshDate();
            #endregion

            FormSta = "A";
            ClearForm();
            bar3.Text = "状态栏";

        }

        public override void InitForm()
        {
            #region 权限处理
            etyUsersModCtrl ey = new etyUsersModCtrl();
            ey.USER_ID = global.basSysPara.UserID;
            ey.MOD_ID = this.MOD_ID;
            global.bzUser.SetFormSecrity(this, barManager1.Items, ey);

            //btnRefresh.ItemClick+=new ItemClickEventHandler(btnRefresh_ItemClick);
            //btnSave.ItemClick+=new ItemClickEventHandler(btnSave_ItemClick);
            //btnNew.ItemClick+=new ItemClickEventHandler(btnNew_ItemClick);
            //btnDelete.ItemClick+=new ItemClickEventHandler(btnDelete_ItemClick);
            btnSec.ItemClick+=new ItemClickEventHandler(btnSec_ItemClick);
            #endregion 

            base.InitForm();
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshDate();
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NewData();
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveDate();
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteDate();
        }

        private void btnSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView1.OptionsFind.AlwaysVisible = !this.gridView1.OptionsFind.AlwaysVisible;
        }

        private void btnExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            F12();
        }
        private void ggcClient_Click(object sender, EventArgs e)
        {
            etyClient ey = srcBinding.Current as etyClient;
            if (ey == null)
                return;
            ClearForm();
            ClearForm();
            TT.Common.formProcess.SetFormEntity(ey, grpDtl);
            cboEnable_Flag.GetCodeText();
            cboCollaborate_Id.GetCodeText();
            cboCustomer_Id.GetCodeText();
            cboFee_Way_Id.GetCodeText();
            FormSta = "E";
        }
        #endregion

        public override Dev.Control.etyQuickKey RefreshDate()
        {
            srcBinding.DataSource = bzClient.GetClient(new etyClient());
            ggcClient.DataSource = srcBinding;
            ggcClient.AutoColumnWidth();
            return base.RefreshDate();
        }

        public override etyQuickKey NewData()
        {
            FormSta = "A";
            etyClient ey = srcBinding.Current as etyClient;
            if (ey != null)
            {
                TT.Common.formProcess.SetFormEntity(ey, grpDtl);

                ClearForm();
                ClearForm();
            }
            return base.NewData();
        }

        public override etyQuickKey DeleteDate()
        {
            etyClient ey = srcBinding.Current as etyClient;
            if (ey == null)
                return null;
            if (TT.HelpInfo.MsgBox.ShowQuestionBox(string.Format("是否删除办单人：{0}？", new string[] { ey.CLIENT_NAM })))
            {
                if (bzClient.Delete_Client(ey).Exec_Success)
                {
                    TT.HelpInfo.MsgBox.ShowSuccessBox("删除成功");
                    lblBar.Caption = "";
                    RefreshDate();
                }
            }
            return base.DeleteDate();
        }

        public override etyQuickKey SaveDate()
        {
            etyClient eyClient = new etyClient();
            TT.Common.formProcess.GetFormEntity(eyClient, grpDtl);
            if (check_data(eyClient))
            {
                daoResult rst;
                if (FormSta == "A")
                    rst = bzClient.Insert_Client(eyClient);
                else
                    rst = bzClient.Update_Client(eyClient);
                if (rst.Exec_Success)
                {
                    TT.HelpInfo.MsgBox.ShowSuccessBox("保存成功");
                    RefreshDate();
                }
                else
                {
                    TT.HelpInfo.MsgBox.ShowWarnBox(rst.Error_String);
                    formProcess.SetControlFocus(rst.Focus_Control, grpDtl);
                }
            }
            return base.SaveDate();
        }

        #region 函数
        private void ClearForm()
        {
            txtClient_Id.Text = "";
            txtClient_Nam.Text = "";
            txtClient_Contact.Text = "";
            cboEnable_Flag.SelectedIndex = 1;
            cboCollaborate_Id.SelectedIndex = 0;
            cboCustomer_Id.SelectedIndex = -1;
            cboFee_Way_Id.SelectedIndex = -1;
            txtClient_Contact1.Text = "";
            txtSeq_No.Text = "";
            cboEnable_Flag.GetCodeText();
            cboCollaborate_Id.GetCodeText();
            cboCustomer_Id.GetCodeText();
            cboFee_Way_Id.GetCodeText();
        }
        private bool check_data(etyClient ey)
        {
            if (string.IsNullOrEmpty(ey.CLIENT_ID.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("办单人ID号不能为空");
                txtClient_Id.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(ey.CLIENT_NAM.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("办单人名称不能为空");
                txtClient_Nam.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(ey.ENABLE_FLAG.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("生效状态不能为空");
                cboEnable_Flag.Focus();
                return false;
            }
            return true;
        }
        #endregion

        private void btnSec_ItemClick(object sender, ItemClickEventArgs e)
        {
            TT.HelpInfo.MsgBox.ShowSuccessFailBox(
            global.bzUser.CreateFormSecrity(this, barManager1.Items, global.basSysPara.UserID, this.MOD_ID)
            );
        }
    }
}
